use std::{pin::Pin, time::Duration};

use bytes::Bytes;
use tools::types::bufs::FromBuf;

use crate::frame::Frame;

#[test]
fn test_frame() {
	let _log = tools::global_defin::trace_log::init_tracing_log();

	let str = r#"INVITE sip:17562140257@39.83.224.180:51781;ob SIP/2.0
Via: SIP/2.0/UDP 183.218.88.21:5080;rport;branch=z9hG4bKtj27eXvrFFQee
Max-Forwards: 70
From: "18620681942oooooooooooo" <sip:18620681942@183.218.88.21>;tag=8DB60187c6pSr
To: <sip:17562140257@39.83.224.180:51781;ob>
Call-ID: f0a03b74-15e9-123e-5497-c400ad673b87
CSeq: 90841965 INVITE
Contact: <sip:mod_sofia@183.218.88.21:5080>
User-Agent: FreeSWITCH-mod_sofia/1.10.11-release+git~20231222T180831Z~f24064f7c9~64bit
Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, INFO, UPDATE, REGISTER, REFER, NOTIFY, PUBLISH, SUBSCRIBE
Supported: timer, path, replaces
Allow-Events: talk, hold, conference, presence, as-feature-event, dialog, line-seize, call-info, sla, include-session-description, presence.winfo, message-summary, refer
Content-Type: application/sdp
Content-Disposition: session
Content-Length: 534
X-FS-Support: update_display,send_info
Remote-Party-ID: "18620681942oooooooooooo" <sip:18620681942@183.218.88.21>;party=calling;screen=yes;privacy=off

v=0
o=FreeSWITCH 1730770824 1730770825 IN IP4 183.218.88.21
s=FreeSWITCH
c=IN IP4 183.218.88.21
t=0 0
m=audio 20690 RTP/AVP 102 8 103 9 0 101 104
a=rtpmap:102 AMR/8000
a=fmtp:102 octet-align=0; mode-set=7
a=rtpmap:8 PCMA/8000
a=rtpmap:103 opus/48000/2
a=fmtp:103 useinbandfec=1; maxaveragebitrate=30000; maxplaybackrate=48000; ptime=20; minptime=10; maxptime=40
a=rtpmap:9 G722/8000
a=rtpmap:0 PCMU/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15
a=rtpmap:104 telephone-event/48000
a=fmtp:104 0-15
a=ptime:20
"#;

	let frame = {
		let string = str.replace('\n', "\r\n");
		let mut from = bytes::Bytes::from(string);

		crate::frame::Frame::from_buf(&mut from)
	}
	.unwrap();

	println!("frame:{:?}", frame);
	println!("body:{:?}", frame.body());
	println!("headers:{:?}", frame.headers());
	// println!("buf:{from:?}");
}

// #[test]
// fn test_v() {
// 	let _log = tools::global_defin::trace_log::init_tracing_log();

// 	let str = r#"SIP/2.0"#;
// 	let mut from = Bytes::from_static(str.as_bytes());

// 	let v = Version::from_buf(&mut from);

// 	println!("frame:{:?}", v);
// }

#[test]
fn test_create() {
	let _log = tools::global_defin::trace_log::init_tracing_log();
	let rt = tools::global_defin::tokio_runtime::get_rt();
	let (tx, mut rx) = tokio::sync::mpsc::channel::<Pin<Box<Frame>>>(10);
	let h = rt.spawn(async move {
		let mut sip = Box::new(Frame::invite("+867967219401@isbc.jx.chinamobile.com"));
		println!("恭恭敬敬a:{:?}",sip.version());

		sip.set_body(Bytes::copy_from_slice("aaaaa".as_bytes()));
		if let Err(e) = tx.try_send(sip.into()) {
			println!("失败:{e}");
		}

		// let sip = Frame::new_request("12345467");
		// if let Err(e) = tx.try_send(sip) {
		// 	println!("失败:{e}");
		// }

		// let sip = Frame::new_request("55555555");
		// if let Err(e) = tx.try_send(sip) {
		// 	println!("失败:{e}");
		// }
	});

	let h2 = rt.spawn(async move {
		loop {
			match rx.recv().await {
				Some(sip) => println!("接收到 sip值:{sip:?}"),
				None => return,
			}
		}
	});

	while !h.is_finished() || !h2.is_finished() {
		std::thread::sleep(Duration::from_secs(1));
	}
}
